package com.work.studio.views;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.work.studio.R;
import com.work.studio.adapters.TodaySignAdapter;
import com.work.studio.entitys.CustomerEntity;
import com.work.studio.entitys.CustomerSignEntity;
import com.work.studio.entitys.TodaySignEntity;
import com.work.studio.recy.CommonAdapter;
import com.work.studio.recy.base.ViewHolder;
import com.work.studio.utils.GlideUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * 用户实时拜访
 */
public class CustomerVisitorViews extends FrameLayout {

    private RecyclerView recyclerView;
    private Context mContext;
//    private CommonAdapter<CustomerSignEntity.CustomerReturnListBean> adapter;
    private TodaySignAdapter adapter;
    private List<TodaySignEntity> datas = new LinkedList<>();
    public CustomerVisitorViews(Context context) {
        this(context, null);
    }

    public CustomerVisitorViews(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerVisitorViews(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.view_customer_visitor, null);
        addView(view);
        this.mContext = context;
        recyclerView = view.findViewById(R.id.recycle_view);
        adapter = new TodaySignAdapter(context, datas);
//        adapter = new CommonAdapter<CustomerSignEntity.CustomerReturnListBean>(context, R.layout.item_customer, datas) {
//            @Override
//            protected void convert(ViewHolder holder, CustomerSignEntity.CustomerReturnListBean entity, int position) {
//
//                holder.setText(R.id.name_value, ""+entity.getUserName());
//                holder.setText(R.id.position_value, ""+entity.getDepartmentName());
//                holder.setText(R.id.det_value, ""+entity.getWorkContent());
//                holder.setText(R.id.address_value, ""+entity.getAddress());
//                List<CustomerSignEntity.CustomerReturnListBean.ImageListBean> imageListBeans = entity.getImageList();
//                if (imageListBeans != null && imageListBeans.size() > 0) {
//                    CustomerSignEntity.CustomerReturnListBean.ImageListBean bean = imageListBeans.get(0);
//                    if (bean != null) {
//                        String path = bean.getResourceUrl();
//                        GlideUtils.loadImage(path, holder.getView(R.id.image_value));
//                    }
//                }
//            }
//        };



//        adapter = new CommonAdapter<CustomerSignEntity.CustomerReturnListBean>(context, R.layout.item_today_sign, datas) {
//            @Override
//            protected void convert(ViewHolder holder, CustomerSignEntity.CustomerReturnListBean entity, int position) {
//
//                ImageView leftTop = holder.getView(R.id.left_top_value);
//                ImageView leftBottom = holder.getView(R.id.left_bottom_value);
//                ImageView rightTop = holder.getView(R.id.right_top_value);
//                ImageView rightBottom = holder.getView(R.id.right_bottom_value);
//                ImageView middle = holder.getView(R.id.middle_value);
//
//                leftTop.setImageResource(R.drawable.left_top_blue);
//                leftBottom.setImageResource(R.drawable.left_bottom_blue);
//                rightTop.setImageResource(R.drawable.right_top_blue);
//                rightBottom.setImageResource(R.drawable.right_bottom_blue);
//                middle.setImageResource(R.drawable.middle_blue);
//
//                int index = new Random().nextInt(6);
//
//                if (index == 0) {
//                    leftTop.setImageResource(R.drawable.left_top_red);
//                } else if (index == 1) {
//                    leftBottom.setImageResource(R.drawable.left_bottom_red);
//                } else if (index == 2) {
//                    rightTop.setImageResource(R.drawable.right_top_red);
//                } else if (index == 3) {
//                    rightBottom.setImageResource(R.drawable.right_bottom_red);
//                } else if (index == 4) {
//                    middle.setImageResource(R.drawable.middle_red);
//                } else if (index == 5) {
//                    leftTop.setImageResource(R.drawable.left_top_red);
//                    rightTop.setImageResource(R.drawable.right_top_red);
//                }
//
//                holder.setText(R.id.name_value, entity.getUserName());
//
//            }
//        };


        GridLayoutManager layoutManager = new GridLayoutManager(context, 4);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int type = datas.get(position).getType();
                if (type == 1) {
                    return 4;
                } else if (type == 2) {
                    return 1;
                }
                return 1;
            }
        });
        recyclerView.setLayoutManager(layoutManager);
//        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));

        LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(mContext, R.anim.layout_animation_fall_down);

        recyclerView.setLayoutAnimation(animation);

        recyclerView.setAdapter(adapter);



    }

//    public void setVistorList(List<CustomerSignEntity.CustomerReturnListBean> customerReturnListBeans) {
//        datas.clear();
//        datas.addAll(customerReturnListBeans);
//        Context context = recyclerView.getContext();
//        LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_fall_down);
//        recyclerView.setLayoutAnimation(controller);
//        recyclerView.getAdapter().notifyDataSetChanged();
//        recyclerView.scheduleLayoutAnimation();
////      adapter.notifyDataSetChanged();
//
//    }

    public void setVistorListT(CustomerEntity customerEntity) {
        datas.clear();

        if (customerEntity != null) {
            List<CustomerEntity.Department1ListBean> department1ListBeans1 = customerEntity.getDepartment1List();
            List<CustomerEntity.Department2ListBean> department1ListBeans2 = customerEntity.getDepartment2List();
            List<TodaySignEntity> entities = new LinkedList<>();
            if (department1ListBeans1 != null && department1ListBeans1.size() > 0) {

                TodaySignEntity todaySignEntity = new TodaySignEntity();
                todaySignEntity.setType(1);
                todaySignEntity.setName("营销一部");
                entities.add(todaySignEntity);
                for (int i = 0 ; i < department1ListBeans1.size() ;i++) {
                    CustomerEntity.Department1ListBean bean = department1ListBeans1.get(i);
                    todaySignEntity = new TodaySignEntity();
                    entities.add(todaySignEntity);
                    todaySignEntity.setType(2);
                    todaySignEntity.setAfternoon_common_daka(bean.isAfternoon_common_daka());
                    todaySignEntity.setAfternoon_customer_daka(bean.isAfternoon_customer_daka());
                    todaySignEntity.setDepartmentName(bean.getDepartmentName());
                    todaySignEntity.setHasDayWork(bean.isHasDayWork());
                    todaySignEntity.setId(bean.getId());
                    todaySignEntity.setMorning_common_daka(bean.isMorning_common_daka());
                    todaySignEntity.setMorning_customer_daka(bean.isMorning_customer_daka());
                    todaySignEntity.setRealName(bean.getRealName());
                }
            }
            //department1ListBeans2 != null && department1ListBeans2.size() > 0
            if (department1ListBeans2 != null && department1ListBeans2.size() > 0) {

                TodaySignEntity todaySignEntity = new TodaySignEntity();
                todaySignEntity.setType(1);
                todaySignEntity.setName("营销二部");
                entities.add(todaySignEntity);

                for (int i = 0 ; i < department1ListBeans2.size() ;i++) {
                    CustomerEntity.Department2ListBean bean = department1ListBeans2.get(i);

                    todaySignEntity = new TodaySignEntity();
                    entities.add(todaySignEntity);
                    todaySignEntity.setType(2);
                    todaySignEntity.setName("营销二部");
                    todaySignEntity.setAfternoon_common_daka(bean.isAfternoon_common_daka());
                    todaySignEntity.setAfternoon_customer_daka(bean.isAfternoon_customer_daka());
                    todaySignEntity.setDepartmentName(bean.getDepartmentName());
                    todaySignEntity.setHasDayWork(bean.isHasDayWork());
                    todaySignEntity.setId(bean.getId());
                    todaySignEntity.setMorning_common_daka(bean.isMorning_common_daka());
                    todaySignEntity.setMorning_customer_daka(bean.isMorning_customer_daka());
                    todaySignEntity.setRealName(bean.getRealName());
                }
            }

            datas.addAll(entities);


        }

//        Context context = recyclerView.getContext();
        LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(mContext, R.anim.layout_animation_fall_down);
        recyclerView.setLayoutAnimation(controller);
        adapter.notifyDataSetChanged();
        recyclerView.scheduleLayoutAnimation();
    }
}
